<template>
  <div class="user-profile">
    <van-nav-bar
    class="app-nav-bar"
    title="个人信息"
    left-arrow
    @click-left="$router.back()"
    >
    </van-nav-bar>
    <input
      type="file"
      hidden
      ref="file"
      accept="image/*"
      @change="onFileChange"
      >
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image
        width="30"
        height="30"
        round
        fit="cover"
        :src="user.photo"
      />
    </van-cell>
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click="isEditNameShow = true"
    >
    </van-cell>
    <van-cell
      title="性别"
      is-link
      :value="user.gender===0 ? '男':'女'"
      @click="isEditGenderShow = true"
      ></van-cell>
    <van-cell title="生日" is-link :value="user.birthday"
      @click="isEditBirthdayShow = true"
    ></van-cell>
    <!-- 修改头像 -->
      <van-popup
        v-if="isEditPhotoShow"
        class="update-photo-popup"
        v-model="isEditPhotoShow"
        position="bottom"
        style="height:100%"
      >
      <update-photo
        :file="previewImage"
        @close="isEditPhotoShow=false"
        @update-photo="user.photo = $event"
        >
      </update-photo>
      </van-popup>
    <van-popup
      v-model="isEditNameShow"
      position="bottom"
      :style="{ height: '100%' }"
      >
        <!-- @update-name="user.name = $event"
        :name="user.name" -->
        <!--
            v-model = "user.name"
            :value ="user.name"
            :input ="user.name = $event"
         -->

         <!--
           多个属性需要保持同步的 使用.sync修饰符
           就能和 :gender.sync="name.gender" 效果与v-model差不多
           @update:gender = "user.gender=$event"
          -->
      <update-name
        v-if="isEditNameShow"
        @close="isEditNameShow = false"
        v-model="user.name"
        />
    </van-popup>
      <!-- 编辑性别 -->
      <van-popup
        v-model="isEditGenderShow"
        position="bottom"
      >
      <update-gender
        v-model="user.gender"
        @close="isEditGenderShow = false"
      >
      </update-gender>
      </van-popup>
      <!-- 编辑生日 -->
      <van-popup
        v-model="isEditBirthdayShow"
        position="bottom"
      >
        <update-birthday
          v-if="isEditBirthdayShow"
          v-model="user.birthday"
          @close="isEditBirthdayShow = false"
        />
      </van-popup>
  </div>
</template>

<script>
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'

import { getUserProfile } from '@/api/user'
export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data () {
    return {
      user: {},
      isEditNameShow: false,
      isEditGenderShow: false,
      isEditBirthdayShow: false,
      isEditPhotoShow: false,
      previewImage: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadUserProfile()
  },
  mounted () {},
  methods: {
    async loadUserProfile () {
      const { data } = await getUserProfile()
      this.user = data.data
    },
    onFileChange () {
      // 清空file的value 相同的文件不触发value
      // 由于formdata中需要传值对象所以我们直接
      const file = this.$refs.file.files[0]
      // const blob = window.URL.createObjectURL(this.$refs.file.files[0])
      this.previewImage = file
      this.isEditPhotoShow = true
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  background-color: #f5f7f9;
}
.update-photo-popup{
  background-color: #000;
}
</style>
